<?php

$page_title = "Stats";
$rank_check = 0;
include "header.inc.php";

$findGeneratedStats = fetch("SELECT * FROM generated_stats WHERE game = '$game'");
if ((!$findGeneratedStats[id]) OR ($findGeneratedStats[generated] <= $timestamp-300))
{
	$numItems     = fetch("SELECT count(id) AS id FROM items2 WHERE game = '$game'");
	$sumShopItems = fetch("SELECT sum(stock) AS stock FROM usershops_items2 WHERE game = 	'$game'");
	$numUserItems = fetch("SELECT count(id) AS id FROM usersitems2 WHERE game = '$game'");
	$numUserItems[id] += $sumShopItems[stock];
	$numPremium   = fetch("SELECT count(id) AS id FROM members2 WHERE premium = '1' AND game = '$game'");
	$numSpecies   = fetch("SELECT count(id) AS id FROM pets2 WHERE game = '$game'");
	$numPets      = fetch("SELECT count(id) AS id FROM user_pets2 WHERE game = '$game'");
	$numUserShops = fetch("SELECT count(id) AS id FROM usershops2 WHERE game = '$game'");
	$numClubs     = fetch("SELECT count(id) AS id FROM clubs2 WHERE game = '$game'");
	$numGalleries = fetch("SELECT count(id) AS id FROM usergallerys WHERE game = '$game'");
	$bdayQ = mysql_query("SELECT * FROM members_profiles2 WHERE game = '$game' AND (birthday LIKE '%$datestamp3%' OR birthday LIKE '%$datestamp4%')");
	$numBdays = mysql_num_rows($bdayQ);
	
	$numMembersM = mysql_num_rows(mysql_query("SELECT id FROM members_profiles2 WHERE game = '$game' AND gender = '2'"));
	$numMembersF = mysql_num_rows(mysql_query("SELECT id FROM members_profiles2 WHERE game = 	'$game' AND gender = '1'"));
	
	function percent($num1, $num2, $int, $dec=0)
	{
	   $sum = $num1+$num2;
	   if($sum==0) return 0;
	   return round(${"num$int"}*100/$sum+0.00000001,$dec);
	}
	
	$percent_boys = percent($numMembersM, $numMembersF, 1);
	$percent_girls = percent($numMembersM, $numMembersF, 2);

	mysql_query("DELETE FROM generated_stats WHERE game = '$game'");
	mysql_query("INSERT INTO generated_stats VALUES ('', '$game', '$timestamp', '$numMembers[id]', '$numBdays', '$getGame[hits]', '$getGame[daily_page_views]', '$numItems[id]', '$numUserItems[id]', '$numUserShops[id]', '$numClubs[id]', '$numGalleries[id]', '$numSpecies[id]', '$numPets[id]', '$percent_boys', '$percent_girls','$numPremium[id]')");

	$findGeneratedStats = fetch("SELECT * FROM generated_stats WHERE game = '$game'");

}

$generated = date("M j @ g:i:s",$findGeneratedStats[generated]);

$findHighLevel = fetch("SELECT name,level FROM user_pets2 WHERE game = '$game' ORDER BY level DESC LIMIT 1");
$findStrongest = fetch("SELECT name,strength FROM user_pets2 WHERE game = '$game' ORDER BY strength DESC LIMIT 1");
$findIntelligence = fetch("SELECT name,intelligence FROM user_pets2 WHERE game = '$game' ORDER BY intelligence DESC LIMIT 1");
$findSpeed = fetch("SELECT name,speed FROM user_pets2 WHERE game = '$game' ORDER BY speed DESC LIMIT 1");
$findDefenisive = fetch("SELECT name,defense FROM user_pets2 WHERE game = '$game' ORDER BY defense DESC LIMIT 1");
$findExperience = fetch("SELECT name,experience FROM user_pets2 WHERE game = '$game' ORDER BY experience DESC LIMIT 1");
$find1PWins = fetch("SELECT name,one_p_wins FROM user_pets2 WHERE game = '$game' ORDER BY one_p_wins DESC LIMIT 1");

// $start_countdown = 1047445459;
// $years_since = floor(($timestamp - $start_countdown) / 31536000);
// $months_since = floor(($timestamp - $start_countdown) / 2592000) - ($years_since * 12);
// $days_since = floor(($timestamp - $start_countdown) / 86400) - ($years_since * 365) - ($months_since * 30);

// if ($years_since == 1) { $year_var = "Year"; } else { $year_var = "Years"; }
// if ($months_since == 1) { $month_var = "Month"; } else { $month_var = "Months"; }
// if ($days_since == 1) { $day_var = "Day"; } else { $day_var = "Days"; }

if ($popup == "true") { print "<html><head><title>$page_title</title></head><body bgcolor=$reallyLight>"; $popupLink = " TARGET=_blank"; }
if ($popup != "true") { print "$openHTML"; }

ECHO <<<END

<br><br>
<P align=center><TABLE BGCOLOR="$topAndBottomBG" CELLSPACING=1 CELLPADDING=0 WIDTH=500>
   <TR>
      <TD COLSPAN=2 WIDTH=500>
         <p><FONT SIZE=-1 COLOR="$topAndBottomText"><B>Site
         Stats</B></FONT></p>
      </TD>
   </TR>
   <TR BGCOLOR="#FFFFFF">
      <TD WIDTH=250>
         <p><B>Site Stats</B></p>
         
         <P><font size=-1>There are $findGeneratedStats[num_users] members<BR>
         $findGeneratedStats[num_prem] <a$popupLink href=upgrade.php?game=$game>Premium Accounts</a><br>
         <a$popupLink href=whoson.php?game=$game&popup=$popup>$countUsersOn</a> members are online!<br>
         <a$popupLink href=birthdays.php?game=$game&popup=$popup>$findGeneratedStats[num_bdays]</a> have birthdays today!<br>
         We have had $findGeneratedStats[num_pviews] Total Page Views<br>
         We have had $findGeneratedStats[num_dpviews] Views today!<br>
         We have a max. of $maxHits hits per day!<br>
         There are $findGeneratedStats[num_items] Items<BR>
         Users own a total $findGeneratedStats[num_uitems] Items<BR>
         There are $findGeneratedStats[num_ushops] User Shops<BR>
         There are $findGeneratedStats[num_uclubs] Clubs<BR>
         There are $findGeneratedStats[num_ugalleries] Galleries<BR>
         </font></P></CENTER>
      </TD>
      <TD WIDTH=250>
         <CENTER><FONT SIZE="+1"><B>Pet Stats</B></FONT>
         
         <P>
         <font size=-1>There are $findGeneratedStats[num_pets] Pet Species<br>
         Users own a total $findGeneratedStats[num_upets] Pets</font></P>
         
         <P>
         <font size=-1>Highest Level: $findHighLevel[name] <FONT SIZE="-1">&#91;$findHighLevel[level]&#93;</FONT><BR>
         Strongest: $findStrongest[name] <FONT SIZE="-1">&#91;$findStrongest[strength]&#93;</FONT><BR>
         Smartest: $findIntelligence[name] <FONT SIZE="-1">&#91;$findIntelligence[intelligence]&#93;</FONT><BR>
         Fastest: $findSpeed[name] <FONT SIZE="-1">&#91;$findSpeed[speed]&#93;</FONT><BR>
         Most Armored: $findDefenisive[name] <FONT SIZE="-1">&#91;$findDefenisive[defense]&#93;</FONT><BR>
		 Most Experience: $findExperience[name] <FONT SIZE="-1">&#91;$findExperience[experience]&#93;</FONT><br>
         Most 1-Player Wins: $find1PWins[name] <FONT SIZE="-1">&#91;$find1PWins[one_p_wins]&#93;</FONT></font><BR>
      </TD>
   </TR>
</TABLE>
</P>

<p align=center>
<font color=blue>$findGeneratedStats[percent_male]% of our users are male!</font><br>
<font color=pink>$findGeneratedStats[percent_female]% of our users are female!</font></p>

<p align=center><b>Last Generated: $generated</b></p>

END;

if ($popup != "true") { print "$closeHTML"; }
if ($popup == "true") { print "<p align=center>&copy;</p></body></html>"; }
?>
